package com.cumt.threeway.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.cumt.threeway.R;
import com.cumt.threeway.constant.URLS;
import com.cumt.threeway.util.ThreeWayData;
import com.cumt.threeway.util.ThreeWayUtil;
import com.cumt.threeway.view.dialog.NeedLoginDialog;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 胡晶晶 on 2016/5/22.
 */
public class SubmitCommentActivity extends FragmentActivity implements View.OnClickListener{

    private EditText mCommentEditText;
    private String mCommentInfo;
    private RequestQueue queue;
    private String mDate;
    private String mName;

    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pinglun);
        initView();
    }

    private void initView() {
        mName = getIntent().getStringExtra("name");
        queue = Volley.newRequestQueue(this);
        mCommentEditText = (EditText) findViewById(R.id.et_comment);
        findViewById(R.id.tv_confirm).setOnClickListener(this);
        findViewById(R.id.btn_back).setOnClickListener(this);
        SimpleDateFormat sDateFormat= new SimpleDateFormat("yyyy-MM-dd");
        mDate = sDateFormat.format(new java.util.Date());
    }

    @Override
    public void onClick(View v) {
        if(v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.tv_confirm:
                if(TextUtils.isEmpty(ThreeWayUtil.getInstance().getLoginAccount(this))) {
                    NeedLoginDialog needLoginDialog = NeedLoginDialog.newInstance("comment");
                    needLoginDialog.show(getSupportFragmentManager(),"needLoginDialog");
                } else if(TextUtils.isEmpty(getEditContent(mCommentEditText))) {
                    ThreeWayUtil.getInstance().showToast(this,getResources().getString(R.string.comment_error));
                } else {
//                    SubmitInfoToServer();
                    ThreeWayData.getInstance().addPinglun(getEditContent(mCommentEditText), mName);
                    setResult(RESULT_OK);
                    finish();
                }
                break;
            case R.id.et_comment:
                mCommentInfo = getEditContent(mCommentEditText);
                break;
        }
    }

    private void SubmitInfoToServer() {
        StringRequest srPost = new StringRequest(Request.Method.POST,  URLS.IP + "NetServlet/GetPingLun",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        String userlogin = response.toString();
                        if (userlogin.equals("true\r\n")) {
                            ThreeWayUtil.getInstance().showToast(SubmitCommentActivity.this, getResources().getString(R.string.comment_success));
                            mHandler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    setResult(RESULT_OK);
                                    finish();
                                }
                            }, 600);
                        } else {
                            finish();
                        }

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {
            // 当手机发送请求时，回调用getParams方法，拼请求
            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("content", getEditContent(mCommentEditText));
                map.put("name", ThreeWayUtil.getInstance().getLoginAccount(SubmitCommentActivity.this));
                map.put("date", mDate);
                return map;
            }
        };
        queue.add(srPost);
    }

    private String getEditContent(EditText editText) {
        return editText.getText().toString();
    }
}
